package com.demo.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 *
 * 监听:  容器启动事件  初始化系统的动作放在这里
 */
@Component
public class SpringApplicationListener implements ApplicationListener<ContextRefreshedEvent> {
    private Logger logger = LoggerFactory.getLogger(SpringApplicationListener.class);

    //测试@Value 注解获取properties里面值
    @Value("${database.url}")
    private String testUrl;


    /**
     * 启动的时候执行的方法
     * @param event
     */
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //可以防止多次执行
       if(event.getApplicationContext().getParent()==null){
           logger.info("=============="+testUrl+"===============");
       }
    }
}
